﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BLL;
using IFactory;
using System.IO;

public partial class UserPlanWeek_UserWeekPlanReturn : System.Web.UI.Page
{
    UserInfoManager dllUserInfoManager = new UserInfoManager();
    BLL.UserPlanWeek dllUserPlanWeek = new UserPlanWeek();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            #region 弹出提醒
            PopupManager dllPopupManager = new PopupManager();
            string _info = dllPopupManager.getPopupInfo();
            if (_info.Length > 0)
            {
                ShowMessage(_info);
            }
            #endregion
            bindGridView();
        }
    }



    /// <summary>
    /// 弹出小窗体事件
    /// </summary>
    /// <param name="msg"></param>
    public void ShowMessage(string msg)
    {
        EeekSoft.Web.PopupWin pop = new EeekSoft.Web.PopupWin();
        this.Controls.Add(pop);
        pop.Text = msg;
        pop.Message = msg;
        pop.Title = System.Configuration.ConfigurationManager.AppSettings["CompanyName"].ToString();
        pop.WindowScroll = true;
        pop.HideAfter = 99999999;
        pop.DockMode = EeekSoft.Web.PopupDocking.BottomLeft;
        pop.OffsetX = int.Parse(Session["_width"].ToString()) - 338;
        pop.ActionType = EeekSoft.Web.PopupAction.MessageWindow;
        pop.OffsetY = 1;
        pop.Width = 220;
        pop.Height = 170;
    }

    /// <summary>
    /// 获得登录用户真实姓名
    /// </summary>
    /// <returns></returns>
    public string getRealName()
    {
        string _realName = "";
        Model.UserInfo objUserInfo = new Model.UserInfo();
        object[] _value = new object[1];
        _value[0] = objUserInfo.UserName;
        System.Data.DataTable _userInfo = dllUserInfoManager.get(BLL.UserInfoManager.field.UserName.ToString(), _value);
        if (_userInfo.Rows.Count > 0)
        {
            _realName = _userInfo.Rows[0]["RealName"].ToString();
        }
        return _realName;
    }

    /// <summary>
    /// 绑定gridview
    /// </summary>
    void bindGridView()
    {
        object[] _value = new object[1];
        Model.UserInfo objUserInfo = new Model.UserInfo();
        _value[0] = objUserInfo.UserName;
        DataTable _weekPlan = dllUserPlanWeek.getWeekPlanReturn(_value);
        DataView dv = new DataView(_weekPlan);
        dv.Sort = "WeekPlanSortId";
        this.gdvWeekPlan.DataSource = dv;
        this.gdvWeekPlan.DataBind();
        #region
        for (int i = 0; i < this.gdvWeekPlan.Rows.Count; i++)
        {
            if (this.gdvWeekPlan.DataKeys[i]["AttachmentPath"].ToString().Length == 0)
            {
                Button _btn = (Button)this.gdvWeekPlan.Rows[i].Cells[12].FindControl("btnDownLoad");
                _btn.Visible = false;
                Label _lbl = (Label)this.gdvWeekPlan.Rows[i].Cells[12].FindControl("lblAlert");
                _lbl.Visible = true;
                _lbl.Text = "无下载";
            }
            wrapByComma(this.gdvWeekPlan, i, 6);
            wrapByComma(this.gdvWeekPlan, i, 7);
            #region 去除特殊符号如&nbsp;
            this.gdvWeekPlan.Rows[i].Cells[1].Text = this.gdvWeekPlan.Rows[i].Cells[1].Text.Replace("&", "");
            this.gdvWeekPlan.Rows[i].Cells[1].Text = this.gdvWeekPlan.Rows[i].Cells[1].Text.Replace("nbsp;", "");
            this.gdvWeekPlan.Rows[i].Cells[1].Text = this.gdvWeekPlan.Rows[i].Cells[1].Text.Replace("amp;", "");

            this.gdvWeekPlan.Rows[i].Cells[2].Text = this.gdvWeekPlan.Rows[i].Cells[2].Text.Replace("&", "");
            this.gdvWeekPlan.Rows[i].Cells[2].Text = this.gdvWeekPlan.Rows[i].Cells[2].Text.Replace("nbsp;", "");
            this.gdvWeekPlan.Rows[i].Cells[2].Text = this.gdvWeekPlan.Rows[i].Cells[2].Text.Replace("amp;", "");

            this.gdvWeekPlan.Rows[i].Cells[3].Text = this.gdvWeekPlan.Rows[i].Cells[3].Text.Replace("&", "");
            this.gdvWeekPlan.Rows[i].Cells[3].Text = this.gdvWeekPlan.Rows[i].Cells[3].Text.Replace("nbsp;", "");
            this.gdvWeekPlan.Rows[i].Cells[3].Text = this.gdvWeekPlan.Rows[i].Cells[3].Text.Replace("amp;", "");

            this.gdvWeekPlan.Rows[i].Cells[4].Text = this.gdvWeekPlan.Rows[i].Cells[4].Text.Replace("&", "");
            this.gdvWeekPlan.Rows[i].Cells[4].Text = this.gdvWeekPlan.Rows[i].Cells[4].Text.Replace("nbsp;", "");
            this.gdvWeekPlan.Rows[i].Cells[4].Text = this.gdvWeekPlan.Rows[i].Cells[4].Text.Replace("amp;", "");

            this.gdvWeekPlan.Rows[i].Cells[5].Text = this.gdvWeekPlan.Rows[i].Cells[5].Text.Replace("&", "");
            this.gdvWeekPlan.Rows[i].Cells[5].Text = this.gdvWeekPlan.Rows[i].Cells[5].Text.Replace("nbsp;", "");
            this.gdvWeekPlan.Rows[i].Cells[5].Text = this.gdvWeekPlan.Rows[i].Cells[5].Text.Replace("amp;", "");

            this.gdvWeekPlan.Rows[i].Cells[6].Text = this.gdvWeekPlan.Rows[i].Cells[6].Text.Replace("&", "");
            this.gdvWeekPlan.Rows[i].Cells[6].Text = this.gdvWeekPlan.Rows[i].Cells[6].Text.Replace("nbsp;", "");
            this.gdvWeekPlan.Rows[i].Cells[6].Text = this.gdvWeekPlan.Rows[i].Cells[6].Text.Replace("amp;", "");

            this.gdvWeekPlan.Rows[i].Cells[7].Text = this.gdvWeekPlan.Rows[i].Cells[7].Text.Replace("&", "");
            this.gdvWeekPlan.Rows[i].Cells[7].Text = this.gdvWeekPlan.Rows[i].Cells[7].Text.Replace("nbsp;", "");
            this.gdvWeekPlan.Rows[i].Cells[7].Text = this.gdvWeekPlan.Rows[i].Cells[7].Text.Replace("amp;", "");

            this.gdvWeekPlan.Rows[i].Cells[8].Text = this.gdvWeekPlan.Rows[i].Cells[8].Text.Replace("&", "");
            this.gdvWeekPlan.Rows[i].Cells[8].Text = this.gdvWeekPlan.Rows[i].Cells[8].Text.Replace("nbsp;", "");
            this.gdvWeekPlan.Rows[i].Cells[8].Text = this.gdvWeekPlan.Rows[i].Cells[8].Text.Replace("amp;", "");

            this.gdvWeekPlan.Rows[i].Cells[9].Text = this.gdvWeekPlan.Rows[i].Cells[9].Text.Replace("&", "");
            this.gdvWeekPlan.Rows[i].Cells[9].Text = this.gdvWeekPlan.Rows[i].Cells[9].Text.Replace("nbsp;", "");
            this.gdvWeekPlan.Rows[i].Cells[9].Text = this.gdvWeekPlan.Rows[i].Cells[9].Text.Replace("amp;", "");

            this.gdvWeekPlan.Rows[i].Cells[10].Text = this.gdvWeekPlan.Rows[i].Cells[10].Text.Replace("&", "");
            this.gdvWeekPlan.Rows[i].Cells[10].Text = this.gdvWeekPlan.Rows[i].Cells[10].Text.Replace("nbsp;", "");
            this.gdvWeekPlan.Rows[i].Cells[10].Text = this.gdvWeekPlan.Rows[i].Cells[10].Text.Replace("amp;", "");

            this.gdvWeekPlan.Rows[i].Cells[11].Text = this.gdvWeekPlan.Rows[i].Cells[11].Text.Replace("&", "");
            this.gdvWeekPlan.Rows[i].Cells[11].Text = this.gdvWeekPlan.Rows[i].Cells[11].Text.Replace("nbsp;", "");
            this.gdvWeekPlan.Rows[i].Cells[11].Text = this.gdvWeekPlan.Rows[i].Cells[11].Text.Replace("amp;", "");

            #endregion
        }
        #endregion
    }

    /// <summary>
    /// 根据逗号换行事件
    /// </summary>
    void wrapByComma(GridView _gridView, int _rowIndex, int _cells)
    {
        string _content = _gridView.Rows[_rowIndex].Cells[_cells].Text;
        _gridView.Rows[_rowIndex].Cells[_cells].Text = _content.Replace("&lt;br&gt;", "<br>");
        //_gridView.Rows[_rowIndex].Cells[_cells].Text = _content.Replace("&gt;", ">");
        _gridView.Rows[_rowIndex].Cells[_cells].Text = _gridView.Rows[_rowIndex].Cells[_cells].Text.Replace(",", "<br>");


    }

    /// <summary>
    /// 下载按钮事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnDownLoad_Click(object sender, EventArgs e)
    {
        int i = 0;
        Button btt = (Button)sender;//获得当前控件
        TableCell cell = (TableCell)btt.Parent;//得到所处的父单元格
        GridViewRow iRow = (GridViewRow)cell.Parent;//得到所处父单元行
        i = iRow.RowIndex;//得到该行的索引
        //读取隐藏地址列
        string path = this.gdvWeekPlan.DataKeys[i]["AttachmentPath"].ToString();
        FileDownload(path);
        // Utility.HttpHelp.DownloadFile(path);
    }


    ///   <summary>   
    ///   文件下载   
    ///   </summary>   
    ///   <param   name="FullFileName"></param>   
    private void FileDownload(string strFileName)
    {

        FileInfo DownloadFile = new FileInfo(strFileName);
        if (!DownloadFile.Exists)
        {
            Utility.HttpHelp.alert("无附件,请与管理员联系", Page);
            return;
        }
        Response.Clear();
        Response.ClearHeaders();
        Response.Buffer = false;
        Response.ContentType = "application/octet-stream";
        Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(strFileName.Substring(strFileName.LastIndexOf('\\') + 1), System.Text.Encoding.UTF8));
        // Response.AppendHeader("Content-Length", DownloadFile.Length.ToString());

        Response.WriteFile(DownloadFile.FullName);
        Response.Flush();
        Response.End();

    }
}